/**
 * 移动端常用的方法工具
 */

let rem = 16
let dpr = window.devicePixelRatio || 1

// 获取随机序列
const getRandom = (ticket) => {
  ticket = ticket ? ticket + '-' : ''
  let now = new Date()
  return ticket + now.getTime() + '-' + (Math.random() * 10).toString(16)
}

// 判断客户端是否iphone
const isIPhone = () => {
  return window.navigator.appVersion.match(/iphone/gi)
}

// 判断客户端是否android
const isAndroid = () => {
  return window.navigator.appVersion.match(/android/gi)
}

// 字体自适应方案一
const setRootFontSize = () => {
  try {
    console.log('mbase')
    let docEl = document.documentElement
    rem = docEl.clientWidth * dpr / 10
    let scale = 1 / dpr
    console.info(rem)

    let metaEl = document.querySelector('meta[name="viewport"]')
    if (!metaEl) {
      metaEl = document.createElement('meta')
      metaEl.setAttribute('name', 'viewport')
      // metaEl.setAttribute('content', 'width=' + dpr * docEl.clientWidth + ', initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no')
      metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no')
      docEl.firstElementChild.insertBefore(metaEl, docEl.firstElementChild.firstElementChild)
    } else {
      metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no')
    }
    docEl.setAttribute('data-dpr', dpr)
    refreshRem()
    if (document.readyState === 'complete') {
      document.body.style.fontSize = 16 * dpr + 'px'
    } else {
      document.addEventListener('DOMContentLoaded', () => {
        document.body.style.fontSize = 16 * dpr + 'px'
      }, false)
    }
  } catch (error) {
    window.alert('设置字体大小失败！')
    console.error(error)
  }
}

const refreshRem = () => {
  console.info('resize')
  rem = document.documentElement.clientWidth / 10
  document.documentElement.style.fontSize = rem + 'px'
}

const responsive = () => {
  let tid = 0
  setRootFontSize()
  window.addEventListener('resize', () => {
    clearTimeout(tid)
    tid = setTimeout(refreshRem(), 300)
  })
}

const rem2px = (value) => {
  value = parseFloat(value) || 0
  return value * rem
}

const px2rem = (value) => {
  value = parseFloat(value) || 0
  return value / rem
}

const lib = {
  getRandom: getRandom,
  isIPhone: isIPhone,
  isAndroid: isAndroid,
  responsive: responsive,
  rem2px: rem2px,
  px2rem: px2rem
}

export {lib, getRandom, isAndroid, isIPhone}

export const COLOR = ['#47B8FE', '#FBB32B', '#7ED1A5', '#FB488A', '#A69AE4',
  '#FA9677', '#BCD932', '#f5ff51',
  '#07a2a4', '#9a7fd1', '#588dd5', '#f5994e', '#c05050',
  '#59678c', '#c9ab00', '#7eb00a', '#6f5553', '#c14089']

export function extend () {
  let options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},
    i = 1,
    length = arguments.length,
    deep = false

  if (typeof target === 'boolean') {
    deep = target
    target = arguments[i] || {}
    i++
  }

  if (typeof target !== 'object' && !isFunction(target)) {
    target = {}
  }

  if (i === length) {
    target = this
    i--
  }

  for (; i < length; i++) {
    if ((options = arguments[i]) != null) {
      for (name in options) {
        src = target[name]
        copy = options[name]

        if (target === copy) {
					continue
				}

        if (deep && copy && (isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
          if (copyIsArray) {
            copyIsArray = false
						clone = src && Array.isArray( src ) ? src : []
          } else {
            clone = src && isPlainObject( src ) ? src : {}
          }
          target[name] = extend(deep, clone, copy)
        } else if (copy !== undefined) {
          target[name] = copy
        }
      }
    }
  }

  return target
}

function isPlainObject (obj) {
  let proto, Ctor

  if (!obj || toString.call(obj) !== '[object Object]') {
    return false
  }

  proto = getProto(obj)

  if (!proto) {
    return true
  }

  Ctor = hasOwn.call(proto, 'constructor') && proto.constructor
  return typeof Ctor === 'function' && fnToString.call(Ctor) === ObjectFunctionString
}

function isFunction (obj) {
  return typeof obj === 'function' && typeof obj.nodeType !== 'number'
}

function hasOwn () {
  return {}.hasOwnProperty
}

const fnToString = hasOwn.toString

const ObjectFunctionString = fnToString.call(Object)

const getProto = Object.getPrototypeOf
